#include "point_datagen.h"


// void check_folder(const char *folder_path) {
//     assert(folder_path != NULL && "Folder path must not be NULL");
//     // 检查文件夹是否存在
//     if (access(folder_path, F_OK) != 0) {
//         printf("Folder '%s' does not exist.\n", folder_path);
//         if (fs::create_directories(folder_path))
//         {
//             printf("fs create dir succ: %s \n", folder_path);
//         }
//     }else {
//         printf("Folder '%s' exists.\n", folder_path);
//     }
// }

// void write_bin_file(const char *filename, uint64_t *buffer, size_t size){
//     FILE *file = fopen(filename, "wb"); // 打开文件，以二进制写入方式打开    
//     if (file != NULL) {        
//         fwrite(buffer, sizeof(uint64_t), size, file); // 将数据写入文件        
//         fclose(file); // 关闭文件    
//     } 
// }


// // 从二进制文件中读取数据到缓冲区
// bool read_bin_file(const char *filename, uint64_t *buffer, size_t size) {
//     FILE *file = fopen(filename, "rb");  // 以二进制读取模式打开文件
//     if (file == NULL) {
//         // 文件打开失败
//         return false;
//     }

//     // 读取数据
//     size_t elementsRead = fread(buffer, sizeof(uint64_t), size, file);
//     fclose(file);  // 关闭文件

//     if (elementsRead != size) {
//         // 读取的元素数量与期望的数量不一致，可能读取失败
//         return false;
//     }

//     return true;
// }

void pt_rand_generator(int point_num, const char *filename){
    int data_num = 0;
    uint64_t* pt_data   = new uint64_t[point_num];
    uint16_t* pt_x = new uint16_t[point_num]; 
    uint16_t* pt_y = new uint16_t[point_num]; 
    uint16_t* pt_z = new uint16_t[point_num]; 
    srand((unsigned int)time(NULL));
    while(data_num < point_num) {
        uint32_t x, y, z;
        uint64_t pt_temp;
        // srand((uint32_t)time(NULL)*100000000000);
        x = rand() % 20 + 1;
        y = rand() % 20 + 1;
        z = rand() % 20 + 1;
        pt_temp = ((z & 0xfff) << 24) + ((y & 0xfff) << 12) + (x & 0xfff);
        // printf(" x= %d, y = %d, z = %d\n", x, y, z);
        if(data_num == 0) {
            pt_data[0] = pt_temp;
            pt_x[0] = x;
            pt_y[0] = y;
            pt_z[0] = z;
            data_num++;
        } else {
            int is_true = 0;
            for(int i = 0; i < data_num; i++){
                if(pt_temp == pt_data[i]) {
                    is_true = 1;
                    break;
                }
            }
            if(is_true == 0)  {
                pt_data[data_num] = pt_temp;
                pt_x[data_num] = x;
                pt_y[data_num] = y;
                pt_z[data_num] = z;
                data_num++;
                printf("@@@ add pt_data, data_num = %d\n", data_num);
            }
        }
    }
    // // print pt_data
    // for (int i = 0; i < point_num; i++){
    //     printf("=== i = %d, pt_data = 0x%llx, pt_z = %d, pt_y = %d, pt_x = %d \n", i, pt_data[i], pt_z[i], pt_y[i], pt_x[i]);
    // }
    write_bin_file(filename, pt_data, point_num);
    delete[] pt_data;
    delete[] pt_x;
    delete[] pt_y;
    delete[] pt_z;
}


void in_rand_generator(int point_num, int inch_loop_num, const char *filename){
    uint64_t* in_data   = new uint64_t[point_num * (inch_loop_num + 1)];
    srand((unsigned int)time(NULL));
    for(int i = 0; i < point_num * (inch_loop_num + 1); i++){
        uint64_t data_h = rand(); //0x01010101;
        uint64_t data_l = rand(); //0x01010101;

        in_data[i] = (data_h<<32) + data_l;
    }
    // for (int i = 0; i < point_num * (inch_loop_num + 1); i++){
    //     printf("=== i = %d, in_data = 0x%llx \n", i, in_data[i]);
    // }
    write_bin_file(filename, in_data, point_num * (inch_loop_num + 1));
    delete[] in_data;
}

void wt_rand_generator(uint32_t DMA_WT_NUM, const char *filename){
    uint64_t* wt_data   = new uint64_t[DMA_WT_NUM];
    srand((unsigned int)time(NULL));
    for(uint32_t i = 0; i < DMA_WT_NUM; i++){
        unsigned long long int data_h = rand(); //0x01010101;//
        unsigned long long int data_l = rand(); //0x01010101;//
        wt_data[i] = (data_h<<32) + data_l;
        // printf("=== i = %d, wt_data = 0x%llx \n", i, wt_data[i]);
    }
    write_bin_file(filename, wt_data, DMA_WT_NUM);
    delete[] wt_data;
}